package com.sale.goods.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.goods.domain.SysDateChange;
import com.sale.goods.domain.dto.SysDateChangeDto;
import com.sale.goods.domain.vo.GetViewVo;
import com.sale.goods.domain.vo.SysDateChangeVo;
import com.sale.goods.mapper.SysDateChangeMapper;
import com.sale.goods.service.GoodPublicSelectService;
import com.sale.goods.service.SysDateChangeService;
import com.sale.storage.api.RemotePublicSelectService;
import com.sale.storage.api.vo.UpdateDateChangeVo;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;

/**
 * 日期变更实现层
 *
 * @author jack
 */
@RequiredArgsConstructor
@Service
public class SysDateChangeServiceImpl implements SysDateChangeService {

    private final SysDateChangeMapper sysDateChangeMapper;

    private final GoodPublicSelectService goodPublicSelectService;

    @DubboReference
    RemotePublicSelectService remotePublicSelectService;

    /**
     * 日期变更分页列表
     */
    @Override
    public TableDataInfo<SysDateChangeDto> getList(SysDateChangeVo sysDateChangeVo, PageQuery pageQuery) {
        Page<SysDateChangeDto> sysDateChangeList = sysDateChangeMapper.getSysDateChangeList(sysDateChangeVo, pageQuery.build());
        return TableDataInfo.build(sysDateChangeList);
    }

    /**
     * 日期变更新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addSysDateChange(SysDateChangeVo sysDateChangeVo) {
        SysDateChange sysDateChange = new SysDateChange();
        BeanUtils.copyProperties(sysDateChangeVo, sysDateChange);
        sysDateChange.setUpdateBy(LoginHelper.getUsername())
            .setUpdateTime(new Date())
            .setCreateTime(new Date());
        int i = sysDateChangeMapper.insert(sysDateChange);
        if (i > 0) {
            UpdateDateChangeVo updateDateChangeVo = new UpdateDateChangeVo();
            updateDateChangeVo.setDocumnetNumber(sysDateChange.getDocumentNumber());
            updateDateChangeVo.setChangeAuditTime(sysDateChange.getChangeAuditTime());
            updateDateChangeVo.setChangeBillingTime(sysDateChange.getChangeBillingTime());
            // 根据变更的数据，将对应单据进行修改
            if (sysDateChangeVo.getBusinessType() == 1) {
                //出库单据
                remotePublicSelectService.outUpdate(updateDateChangeVo);
            } else if (sysDateChangeVo.getBusinessType() == 2) {
                //入库单据
                remotePublicSelectService.inUpdate(updateDateChangeVo);
            } else if (sysDateChangeVo.getBusinessType() == 3) {
                //库内单据
                remotePublicSelectService.kuneUpdate(updateDateChangeVo);
            } else {
                //财务单据
                goodPublicSelectService.goodUpdate(updateDateChangeVo);
            }
        }
        return i > 0;
    }

    /**
     * 查询获取单据详情
     */
    @Override
    public SysDateChangeDto getView(GetViewVo getViewVo) throws ParseException {
        SysDateChangeDto sysDateChangeDto = new SysDateChangeDto();
        if (getViewVo.getBusinessType() == 1) {
            //出库单据
            com.sale.storage.api.dto.SysDateChangeDto sysDateChangeDto1 = remotePublicSelectService.outSelect(getViewVo.getDocumentNumber());
            if (sysDateChangeDto1.getDocumentType() != null) {
                BeanUtils.copyProperties(sysDateChangeDto1, sysDateChangeDto);
                sysDateChangeDto.setDocumentNumber(getViewVo.getDocumentNumber());
            } else {
                throw new GlobalException("未查询到该单据");
            }
            return sysDateChangeDto;
        } else if (getViewVo.getBusinessType() == 2) {
            //入库单据
            com.sale.storage.api.dto.SysDateChangeDto sysDateChangeDto1 = remotePublicSelectService.inSelect(getViewVo.getDocumentNumber());
            if (sysDateChangeDto1.getDocumentType() != null) {
                BeanUtils.copyProperties(sysDateChangeDto1, sysDateChangeDto);
                sysDateChangeDto.setDocumentNumber(getViewVo.getDocumentNumber());
            } else {
                throw new GlobalException("未查询到该单据");
            }
            return sysDateChangeDto;
        } else if (getViewVo.getBusinessType() == 3) {
            //库内单据
            com.sale.storage.api.dto.SysDateChangeDto sysDateChangeDto1 = remotePublicSelectService.kuneSelect(getViewVo.getDocumentNumber());
            if (sysDateChangeDto1.getDocumentType() != null) {
                BeanUtils.copyProperties(sysDateChangeDto1, sysDateChangeDto);
                sysDateChangeDto.setDocumentNumber(getViewVo.getDocumentNumber());
            } else {
                throw new GlobalException("未查询到该单据");
            }
            return sysDateChangeDto;
        } else {
            //财务单据
            SysDateChangeDto sysDateChangeDto1 = goodPublicSelectService.goodSelect(getViewVo.getDocumentNumber());
            if (sysDateChangeDto1.getDocumentType() != null) {
                BeanUtils.copyProperties(sysDateChangeDto1, sysDateChangeDto);
                sysDateChangeDto.setDocumentNumber(getViewVo.getDocumentNumber());
            } else {
                throw new GlobalException("未查询到该单据");
            }
            return sysDateChangeDto;
        }
    }
}
